package main

import (
	"fmt"
	"sync"
)

var mu sync.Mutex
var r = Resource{1, 0}

type Resource struct {
	val     int
	version int
}

func pp(version int, w *sync.WaitGroup) {
	defer w.Done()
	for {
		mu.Lock()
		if r.version == version {
			fmt.Println(version, ":", r.val)
			r.val++
			r.version = (r.version + 1) % 3
		}
		if r.val > 100 {
			mu.Unlock()
			break
		}
		mu.Unlock()
	}
}

func main() {
	wait := &sync.WaitGroup{}
	wait.Add(3)
	go pp(0, wait)
	go pp(1, wait)
	go pp(2, wait)
	wait.Wait()
}
